package flex.cc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import flex.cc.PublicUtil;
import flex.cc.base.dto.LoginAuthDto;
import flex.cc.base.vo.PageInfo;
import flex.cc.system.dao.NoDisturbDao;
import flex.cc.system.entity.NoDisturbEntity;
import flex.cc.system.service.NoDisturbService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service("noDisturbService")
public class NoDisturbServiceImpl extends ServiceImpl<NoDisturbDao, NoDisturbEntity> implements NoDisturbService {


    @Override
    public boolean saveNoDisturb(NoDisturbEntity noDisturb, LoginAuthDto loginAuthDto) {
        noDisturb.setUpdateInfo(loginAuthDto);
        noDisturb.setComCode(loginAuthDto.getComCode());
        boolean saveResult = this.save(noDisturb);
        if (!saveResult) {
            log.info("免打扰id【 {} 】保存信息失败", noDisturb.getId());
        }
        return saveResult;
    }

    @Override
    public boolean modifyNoDisturb(NoDisturbEntity noDisturb, LoginAuthDto loginAuthDto) {
        noDisturb.setUpdateInfo(loginAuthDto);
        noDisturb.setVersion(noDisturb.getVersion() + 1);
        boolean updateResult = this.updateById(noDisturb);
        if (!updateResult) {
            log.info("免打扰id【 {} 】修改信息失败", noDisturb.getId());
        }
        return updateResult;
    }

    @Override
    public boolean modifyStatusById(NoDisturbEntity noDisturb, LoginAuthDto loginAuthDto) {
        NoDisturbEntity noDisturbEntity = queryNoDisturbById(noDisturb.getId());
        if (!PublicUtil.isEmpty(noDisturbEntity)) {
            noDisturbEntity.setStatus(noDisturb.getStatus());
            noDisturbEntity.setVersion(noDisturbEntity.getVersion()+1);
            noDisturbEntity.setUpdateInfo(loginAuthDto);
            boolean updateResult = this.updateById(noDisturbEntity);
            if (!updateResult) {
                log.info("免打扰id【 {} 】修改状态失败", noDisturb.getId());
            }
            return updateResult;
        }
        return false;
    }

    @Override
    public boolean removeNoDisturbById(Long noDisturbId) {
        boolean removeResult = this.removeById(noDisturbId);
        if (!removeResult) {
            log.info("免打扰id【 {} 】删除信息失败", noDisturbId);
        }
        return removeResult;
    }

    @Override
    public NoDisturbEntity queryNoDisturbById(Long noDisturbId) {
        NoDisturbEntity byId = this.getById(noDisturbId);
        return byId;
    }

    @Override
    public List<NoDisturbEntity> queryNoDisturbList(NoDisturbEntity noDisturb) {
        QueryWrapper<NoDisturbEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(PublicUtil.isNotEmpty(noDisturb.getPhoneNum()), "phone_num", noDisturb.getPhoneNum())
                .like(PublicUtil.isNotEmpty(noDisturb.getCustomerName()), "customer_name", noDisturb.getCustomerName())
                .like(PublicUtil.isNotEmpty(noDisturb.getSysCode()), "sys_code", noDisturb.getSysCode())
                .like(PublicUtil.isNotEmpty(noDisturb.getCreator()), "creator", noDisturb.getCreator())
                .eq(PublicUtil.isNotEmpty(noDisturb.getComCode()),"com_code", noDisturb.getComCode())
                .eq("status", "1")  //启用状态
                .orderByDesc("created_time");
        List<NoDisturbEntity> noDisturbEntitys = this.list(queryWrapper);
        return noDisturbEntitys;
    }

    @Override
    public IPage<NoDisturbEntity> queryNoDisturbListWithPage(NoDisturbEntity noDisturb) {
        Page<NoDisturbEntity> page = new Page<>(noDisturb.getPageNum(),noDisturb.getPageSize());
        QueryWrapper<NoDisturbEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(PublicUtil.isNotEmpty(noDisturb.getPhoneNum()), "phone_num", noDisturb.getPhoneNum())
                .like(PublicUtil.isNotEmpty(noDisturb.getCustomerName()), "customer_name", noDisturb.getCustomerName())
                .like(PublicUtil.isNotEmpty(noDisturb.getSysCode()), "sys_code", noDisturb.getSysCode())
                .like(PublicUtil.isNotEmpty(noDisturb.getCreator()), "creator", noDisturb.getCreator())
                .eq(PublicUtil.isNotEmpty(noDisturb.getComCode()),"com_code", noDisturb.getComCode())
                .orderByDesc("created_time");
        IPage<NoDisturbEntity> noDisturbEntityIPage = this.page(page,queryWrapper);
        return noDisturbEntityIPage;
    }
}
